package com.sx.art2.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sx.art2.beans.Account;
import com.sx.art2.beans.ShangjiaAccount;
import com.sx.art2.mapper.AccountMapper;
import com.sx.art2.mapper.ShangjiaAccountMapper;
import com.sx.art2.service.AccountService;
@Service
public class AccountServiceimpl implements AccountService {
	@Autowired
	AccountMapper accountMapper;
	@Autowired
	ShangjiaAccountMapper shangjiaAccountMapper;
	@Override
	public List<Account> list(int userId) {
		// TODO Auto-generated method stub
		return accountMapper.list(userId);
	}

	@Override
	@Transactional
	public void add(Account account,int sign) {
		// TODO Auto-generated method stub
		accountMapper.add(account);
		int objectId=accountMapper.selectAccountId(account.getUserId());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ShangjiaAccount shangjiaAccount=new ShangjiaAccount();
		if(sign==1) {
			shangjiaAccount.setAccountBalance(shangjiaAccountMapper.selectBalance()+account.getAccountTrade());
			shangjiaAccount.setAccountTrade(account.getAccountTrade());
			shangjiaAccount.setAccountType(1);
			shangjiaAccount.setAccountDate(df.format(new Date()));
			shangjiaAccount.setUserId(account.getUserId());
			shangjiaAccount.setObjectId(objectId+"");
			shangjiaAccountMapper.add(shangjiaAccount);
		}if(sign==2) {
			shangjiaAccount.setAccountBalance(shangjiaAccountMapper.selectBalance()-account.getAccountTrade());
			shangjiaAccount.setAccountTrade(account.getAccountTrade());
			shangjiaAccount.setAccountType(2);
			shangjiaAccount.setAccountDate(df.format(new Date()));
			shangjiaAccount.setUserId(account.getUserId());
			shangjiaAccount.setObjectId(objectId+"");
			shangjiaAccountMapper.add(shangjiaAccount);
		}
		
		
	}

	@Override
	public List<Account> listAll() {
		// TODO Auto-generated method stub
		return accountMapper.listAll();
	}



	

}
